<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/7/17
 * Time: 11:47
 */

namespace app\modules\api\controllers;


use app\models\Order;
use app\models\UploadConfig;
use app\models\UploadForm;
use app\models\Agent;
use app\models\OrderDetail;
use app\models\Vip;
use app\models\Goods;
use app\models\User;
use app\models\Super;
use app\modules\api\behaviors\LoginBehavior;
use app\modules\api\models\ExpressDetailForm;
use app\modules\api\models\LocationForm;
use app\modules\api\models\OrderClerkForm;
use app\modules\api\models\OrderCommentForm;
use app\modules\api\models\OrderCommentPreview;
use app\modules\api\models\OrderConfirmForm;
use app\modules\api\models\OrderDetailForm;
use app\modules\api\models\OrderListForm;
use app\modules\api\models\OrderPayDataForm;
use app\modules\api\models\OrderRefundDetailForm;
use app\modules\api\models\OrderRefundForm;
use app\modules\api\models\OrderRefundPreviewForm;
use app\modules\api\models\OrderRevokeForm;
use app\modules\api\models\OrderSubmitForm;
use app\modules\api\models\OrderSubmitPreviewForm;
use app\modules\api\models\QrcodeForm;
use yii\base\Object;
use yii\helpers\VarDumper;

class OrderController extends Controller
{
    public function behaviors()
    {
        return array_merge(parent::behaviors(), [
            'login' => [
                'class' => LoginBehavior::className(),
            ],
        ]);
    }

    //订单提交前的预览页面
    public function actionSubmitPreview()
    {
        $pingtai_id=1;
        $res = Agent::find()->where(['store_id'=>$this->store->id])->select('pingtai_id')->asArray()->all();
        if($res[0]['pingtai_id']!=''){
            $pingtai_id = $res[0]['pingtai_id'];
        }
        $form = new OrderSubmitPreviewForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->pingtai_id = $pingtai_id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //订单提交
    public function actionSubmit()
    {
        $form = new OrderSubmitForm();
        $model = \Yii::$app->request->post();
        if($model['offline'] == 0){
            $form->scenario = "EXPRESS";
        }else{
            $form->scenario = "OFFLINE";
        }
        $pingtai_id=1;
        $res = Agent::find()->where(['store_id'=>$this->store->id])->select('pingtai_id')->asArray()->all();
        if($res[0]['pingtai_id']!=''){
            $pingtai_id = $res[0]['pingtai_id'];
        }
//        echo $pingtai_id;
        $form->attributes = $model;
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $form->pingtai_id = $pingtai_id;
        $form->version = $this->version;
        $this->renderJson($form->save());
    }

    //订单支付数据
    public function actionPayData()
    {
        $pingtai_id=1;
        $res = Agent::find()->where(['store_id'=>$this->store->id])->select('pingtai_id')->asArray()->all();
        if($res[0]['pingtai_id']!=''){
            $pingtai_id = $res[0]['pingtai_id'];
        }
        $form = new OrderPayDataForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user = \Yii::$app->user->identity;
        $form->pingtai_id = $pingtai_id;
        $this->renderJson($form->search());
    }

    //订单列表
    public function actionList()
    {
        $form = new OrderListForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //订单取消
    public function actionRevoke()
    {
        $form = new OrderRevokeForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->save());
    }

    //订单确认收货
    public function actionConfirm()
    {
        $form = new OrderConfirmForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->save());
    }

    //订单各个状态数量
    public function actionCountData()
    {
        $res = OrderListForm::getCountData($this->store->id, \Yii::$app->user->id);
        return $this->renderJson([
            'code' => 0,
            'msg' => 'success',
            'data' => $res,
        ]);
    }


    //订单详情
    public function actionDetail()
    {
        $form = new OrderDetailForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //售后页面
    public function actionRefundPreview()
    {
        $form = new OrderRefundPreviewForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //售后提交
    public function actionRefund()
    {
        $form = new OrderRefundForm();
        $form->attributes = \Yii::$app->request->post();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->save());
    }

    //售后订单详情
    public function actionRefundDetail()
    {
        $form = new OrderRefundDetailForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //评论预览页面
    public function actionCommentPreview()
    {
        $form = new OrderCommentPreview();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }

    //评论提交
    public function actionComment()
    {
        $form = new OrderCommentForm();
        $form->attributes = \Yii::$app->request->post();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->save());
    }

    //订单物流信息
    public function actionExpressDetail()
    {
        $form = new ExpressDetailForm();
        $form->attributes = \Yii::$app->request->get();
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->search());
    }
    //核销订单
    public function actionClerk()
    {
        $form = new OrderClerkForm();
        $form->order_id = \Yii::$app->request->get('order_id');
        $form->store_id = $this->store->id;
        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->save());
    }
    //核销订单详情
    public function actionClerkDetail()
    {
        if(\Yii::$app->user->identity->is_clerk != 1){
            $this->renderJson([
                'code' => 1,
                'msg' => '不是核销员禁止核销'
            ]);
        }
        $form = new OrderDetailForm();
        $form->order_no = \Yii::$app->request->get('order_no');
        $form->store_id = $this->store->id;
//        $form->user_id = \Yii::$app->user->id;
        $this->renderJson($form->clerk());
    }
    public function actionGetQrcode()
    {
        $order_no = \Yii::$app->request->get('order_no');
        $order = Order::findOne(['order_no'=>$order_no,'store_id'=>$this->store->id]);
        $form = new QrcodeForm();
//        $form->order_no = $order_no;
        $form->data = [
            'scene'=>"{$order_no}",
            'page'=>"pages/clerk/clerk",
            'width'=>100
        ];
        $form->store = $this->store;
        $res = $form->getQrcode();
        return json_encode($res,JSON_UNESCAPED_UNICODE);
    }

    public function actionLocation()
    {
        $form = new LocationForm();
        $form->store_id = $this->store->id;
        $form->attributes = \Yii::$app->request->get();
        $this->renderJson($form->search());
    }
    //支付成功之后应该弹出哪种窗口
    public function actionBoom()
    {
        $order_id = \Yii::$app->request->get('order_id');
        $store_id = \Yii::$app->request->get('store_id');
        $user_id = \Yii::$app->user->id;
        $pingtai_id = 1;
        $res = Agent::find()->where(['store_id' => $store_id])->select('pingtai_id')->asArray()->all();
        if ($res[0]['pingtai_id'] != '') {
            $pingtai_id = $res[0]['pingtai_id'];
        }
        $orders = Order::findOne([
            'id' => $order_id,
        ]);
        $super_condition = Super::find()->where(['store_id' => $pingtai_id, 'type' => 1])->asArray()->all();
//        if($super_condition){
//            $super_money = $super_condition[0]['money'];
//        }
        $is_vip = User::findOne(['id' => $user_id]);
//        if ( !$super_condition||$orders->pay_price<$super_condition[0]['money']) {
        if ( $is_vip->is_distributor==0) {
            $condition = Vip::find()->where(['store_id' => $pingtai_id, 'type' => 1])->asArray()->all();

            if (!$condition) {
                $this->renderJson([
                    'code' => 0,
                    'msg' => '商家还未开通Vip',
                ]);
                return;
            }

            if ($is_vip->is_vip != 0) {
                $this->renderJson([
                    'code' => 0,
                    'msg' => '该用户已成为vip',
                ]);
                return;
            }

            $cat = $condition[0]['cat_id'];
            $money = $condition[0]['money'];
            $num = 0;
            $price = 0;
            $order = OrderDetail::find()->alias('od')->leftJoin(['good' => Goods::tableName()], 'good.id = od.goods_id')
                ->select('good.cat_id,od.total_price')->where(['order_id' => $order_id])->asArray()->all();
            foreach ($order as $value) {
                if ($value['cat_id'] == $cat) {
                    $price = $value['total_price']+$price;
                }
            }
            if ($price >= $money) {
                if(User::updateAll(['is_vip'=>3],['store_id'=>$store_id,'id'=>$user_id])) {
                    $this->renderJson([
                        'code' => 2,
                        'msg' => '可弹窗',
                    ]);
                }else{
                    return [
                        'code' => 1,
                        'msg' => 'user 表存储失败',
                    ];
                }

            } else {
                $this->renderJson([
                    'code' => 0,
                    'msg' => '不可弹窗',
                ]);
            }
        }else if($super_condition&&$is_vip->is_distributor==1){
            if ($is_vip->is_super != 0) {
                $this->renderJson([
                    'code' => 0,
                    'msg' => '该用户已成超级分享家',
                ]);
                return;
            }
            $user = User::findOne(['id'=>$user_id,'store_id'=>$store_id]);
            $user->is_super = 3;
            $user->save();

            $this->renderJson([
                'code'=>1,
                'msg'=>'可弹窗',
            ]);
        }
    }
}